package com.lyy.yygh.cmn.controller;


import com.lyy.yygh.cmn.service.DictService;
import com.lyy.yygh.common.result.Result;
import com.lyy.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(value = "数据字典接口")
@RestController
@RequestMapping("/admin/cmn/dict")
//@CrossOrigin
public class DictController {

    @Autowired
    private DictService dictService;


//    根据dictcode查询下面的节点
    @ApiOperation(value = "根据dictcode查询下面的节点")
    @GetMapping(value = "/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
       List<Dict> list= dictService.findByDictCode(dictCode);
       return Result.ok(list);

    }

    /**
     * 导入数据
     *
     */

    @ApiOperation(value = "导入")
    @PostMapping("importData")
    public Result importData(MultipartFile file) {
        dictService.importData(file);
        return Result.ok();
    }

    /**
     * 导出数据到excel
     * @param response
     */
    @GetMapping("exportData")
    public void exportdata(HttpServletResponse response) {

        dictService.exportData(response);
        //return Result.ok();
    }

    //根据数据id查询子数据
    @ApiOperation(value = "根据数据id查询子数据")
    @GetMapping("findChildrenData/{id}")
    public Result findChildrenData(@PathVariable Long id) {
        List<Dict> list = dictService.findChildrenData(id);
        return Result.ok(list);


    }

    //根据dictcode和value查询
    @GetMapping("getName/{dictCode}/{value}")
    public String getname(@PathVariable String dictCode,

                          @PathVariable String value){
        System.out.println("dictcode是"+dictCode);
        System.out.println("value"+value);
        String dicyName=dictService.getDictName(dictCode,value);
        System.out.println("dicyName"+dicyName);
        return dicyName;

    }
    //根据value查询
    @GetMapping("getName/{value}")
    public String getname(
                          @PathVariable String value){
        String dicyName=dictService.getDictName("",value);
        return dicyName;

    }

}
